package com.hiwan.unions.activity.system.newfragment;

import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.hiwan.unions.R;
import com.hiwan.unions.activity.base.mvpbase.BaseActivity;
import com.hiwan.unions.activity.system.newfragment.Contract.NewsListContract;
import com.hiwan.unions.activity.system.newfragment.presenter.NewsListPresenter;
import com.hiwan.unions.adapter.bean.NewsBean;
import com.hiwan.unions.adapter.fragmentAdapters.NewsAdapter;
import com.hiwan.unions.util.CheckUtils;
import com.hiwan.unions.util.JsonUtil;
import com.hiwan.unions.util.Utils;
import com.hiwan.unions.view.PullToRefreshLayout;
import com.jwenfeng.library.pulltorefresh.BaseRefreshListener;
import com.jwenfeng.library.pulltorefresh.ViewStatus;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by llg on 2017/7/8.
 */
public class NewsActivity extends BaseActivity<NewsListPresenter> implements NewsListContract.View, View.OnClickListener {
    PullToRefreshLayout pullToRefreshLayout;
    private ListView listView;
    private int content = 1;
    private NewsAdapter adpater;
    private List<NewsBean> cDates = new ArrayList<NewsBean>();

    @Override
    protected int getLayout() {
        return R.layout.activity_news;
    }

    @Override
    protected void initData() {
        content = 1;
        cDates.clear();
        mPresenter.getOrderData(this, content, true);
    }

    @Override
    protected void initInject() {
        mPresenter = new NewsListPresenter();
    }

    @Override
    protected void onViewCreated() {
        super.onViewCreated();
        setTitleTxt("我的消息");
        listView = findView(R.id.myListview);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (!cDates.get(position).getType().equals("3")) {
                    Utils.callHelpDeilsActivity(CheckUtils.checkAddressUrl(cDates.get(position).getModel_url()), NewsActivity.this, "");
                }
            }
        });
        adpater = new NewsAdapter(this, cDates);
        listView.setAdapter(adpater);
        pullToRefreshLayout = findView(R.id.refreshLayout_fragment);
        pullToRefreshLayout.setRefreshListener(new BaseRefreshListener() {
            @Override
            public void refresh() {
                content = 1;
                cDates.clear();
                mPresenter.getOrderData(mContext, content, false);
            }

            @Override
            public void loadMore() {
                content++;
                mPresenter.getOrderData(mContext, content, false);
            }
        });
    }

    /**
     * 加载完成的状态
     */
    private void showContentView() {
        pullToRefreshLayout.showView(ViewStatus.CONTENT_STATUS);
    }


    /**
     * 加载失败点击重新加载的状态
     */
    private void showeMPTY(String text) {
        pullToRefreshLayout.finishRefresh();
        pullToRefreshLayout.finishLoadMore();
        pullToRefreshLayout.setCanLoadMore(false);
        pullToRefreshLayout.showView(ViewStatus.EMPTY_STATUS);
        View error = pullToRefreshLayout.getView(ViewStatus.EMPTY_STATUS);
        TextView empty_text = findView(error, R.id.empty_text);
        empty_text.setText(text);
    }

    /**
     * 网络显示错误
     */
    private void showError() {
        // 结束刷新
        pullToRefreshLayout.finishRefresh();
        pullToRefreshLayout.finishLoadMore();
        pullToRefreshLayout.showView(ViewStatus.ERROR_STATUS);
        View error = pullToRefreshLayout.getView(ViewStatus.ERROR_STATUS);
        findView(error, R.id.shuax).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        initData();
    }

    @Override
    public void onOrderList(JSONArray jsonArray, boolean type) {
        pullToRefreshLayout.finishRefresh();
        pullToRefreshLayout.finishLoadMore();
        if (type) {
            pullToRefreshLayout.setCanLoadMore(false);
        } else {
            pullToRefreshLayout.setCanLoadMore(true);
        }
        showContentView();
        for (int i = 0; i < jsonArray.length(); i++) {
            JSONObject jsonObject = JsonUtil.getJSONObject(jsonArray, i);
            NewsBean bean = new NewsBean();
            bean.setTitle(JsonUtil.getString(jsonObject, "title"));
            bean.setCreate_time(JsonUtil.getString(jsonObject, "create_time"));
            bean.setContent(JsonUtil.getString(jsonObject, "content"));
            bean.setType(JsonUtil.getString(jsonObject, "type"));
            bean.setModel_url(JsonUtil.getString(jsonObject, "model_url"));
            bean.setModel(JsonUtil.getString(jsonObject, "model"));
            cDates.add(bean);
        }
        adpater.notifyDataSetChanged();
    }

    @Override
    public void showErrorMsg(String msg, boolean type) {
        if (type) {
            showError();
        } else {
            showeMPTY(msg);
        }
    }

    @Override
    public void loadSuccess() {

    }
}
